package com.demo.logistics.stockControl.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.demo.logistics.R;
import com.demo.logistics.R.id;
import com.demo.logistics.entities.Products;
import com.demo.logistics.stockControl.IStockReceiveView;
import com.demo.logistics.stockControl.presenter.StockPresenter;

public class StockReceiveView extends StockView implements IStockReceiveView {

	private StockPresenter _present;
	private Spinner spnProd;
	private EditText edQty;
	private EditText edLorryNo;
	private EditText edDriverName;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stock_receive_view);
		super.bindLogoEvent();
		spnProd = (Spinner)findViewById(R.id.spn_product);
		edQty = (EditText)findViewById(R.id.txt_qty);
		edLorryNo = (EditText)findViewById(R.id.txt_lorry_no);
		edDriverName = (EditText)findViewById(R.id.txt_driver);
		_present = new StockPresenter(this);
		bindProductData();
		bindSubmitButtonEvent();
		bindSubmitDoneButtonEvent();
	}

	@Override
	public void bindProductData() {
		// TODO Auto-generated method stub
		String[] allProdNames = _present.fetchAllProductNames();
		if(allProdNames != null){
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, allProdNames);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
			spnProd.setAdapter(adapter);
			spnProd.setEnabled(true);
		}
		else{
			spnProd.setEnabled(false);
		}
	}

	@Override
	public void bindSubmitButtonEvent() {
		Button btn = (Button) findViewById(R.id.btn_submit);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				boolean valid = _present.handleStockSubmitBtn(spnProd.isEnabled()==true?spnProd.getSelectedItem().toString():null, edQty.getText().toString(), edLorryNo.getText().toString(), edDriverName.getText().toString());
				if(valid){
					startActivity(new Intent(getApplicationContext(), StockReceiveView.class));
				}
				else{
					displayErrorMessage(_present.getErrorMsg());
				}
			}
		});
	}
	
	@Override
	public void bindSubmitDoneButtonEvent() {
		Button btn = (Button) findViewById(R.id.btn_submit_done);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				boolean valid = _present.handleStockSubmitBtn(spnProd.getSelectedItem().toString(), edQty.getText().toString(), edLorryNo.getText().toString(), edDriverName.getText().toString());
				if(valid){
					startActivity(new Intent(getApplicationContext(), StockListView.class));
				}
				else{
					displayErrorMessage(_present.getErrorMsg());
				}
			}
		});
	}
	
	@Override
	public void onStop(){
		//TODO save the new name to file
		super.onStop();
	}
}
